package com.itheima.controller;


import com.github.pagehelper.PageInfo;

import com.itheima.pojo.Result;
import com.itheima.pojo.StatusCode;
import com.itheima.pojo.SysDictData;
import com.itheima.pojo.SysDictDataVO;
import com.itheima.service.SysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/sysDictData")
public class SysDictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;

    //1.添加字典
    @PostMapping("/add")
    public Result add(@RequestBody SysDictDataVO sysDictDataVO){

        try {
            sysDictDataService.add(sysDictDataVO);
            return new Result(StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"添加失败");
        }
    }

    //2.删除选中字典
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<SysDictDataVO> sysDictDataVOList){

        try {
            sysDictDataService.deleteBatch(sysDictDataVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"删除失败");
        }
        return new Result(StatusCode.OK,"删除成功");
    }

    //3.编辑字典
    @PostMapping("/edit")
    public Result edit(@RequestBody SysDictDataVO sysDictDataVO){

        try {
            sysDictDataService.edit(sysDictDataVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,"更新失败");
        }
        return new Result(StatusCode.OK,"更新成功");
    }

    //4.根据字典类型uid获取字典列表   dictLabel: ""
    //dictTypeUid: "1294620562944958464"
    @PostMapping("/getList")
    public Map<String, Object> getList(@RequestBody SysDictDataVO sysDictDataVO){

        Map<String,Object> result = new HashMap<>();
        Map<String,Object> recordsMap = null;
        try {
            PageInfo<SysDictData> pageInfo = sysDictDataService.getList(sysDictDataVO);
            recordsMap = new HashMap<>();
            recordsMap.put("records",pageInfo.getList());
            recordsMap.put("total",pageInfo.getTotal());
            recordsMap.put("size",sysDictDataVO.getPageSize());
            recordsMap.put("current",sysDictDataVO.getCurrentPage());
            recordsMap.put("orders",null);
            recordsMap.put("optimizeCountSql",true);
            recordsMap.put("isSearchCount",true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回结果
        result.put("code","success");
        result.put("data",recordsMap);
        return result;
    }

    //5.根据字典类型获取字典列表
    @PostMapping("/getListByDictType")
    public Map getListByDictType(@RequestParam("dictType") String dictType){

        List<Map> listByDictType = sysDictDataService.getListByDictType(dictType);

        Map<String,Object> result = new HashMap<>();

        HashMap<String,Object> map1 = new HashMap<>();
        map1.put("list",listByDictType);

        result.put("code","success");
        result.put("data",map1);
        return result;
    }

    @PostMapping("/getListByDictTypeList")
    public Map getListByDictTypeList(@RequestBody String[] dictTypeList){

        List<Map> listByDictTypeList = sysDictDataService.getListByDictTypeList(dictTypeList);

        HashMap<String,Object> result = new HashMap<>();

        HashMap<String,Object> map1 = new HashMap<>();

        map1.put("defaultValue",1);
        map1.put("list",listByDictTypeList);

        HashMap<String,Object> map2 = new HashMap<>();
        map2.put("sys_account_source",map1);

        //封装数据,返回结果
        result.put("code","success");
        result.put("data",map2);
        return result;
    }
}
